﻿/// Copyright (c) 2012 John Tear
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
/// and associated documentation files (the "Software"), to deal in the Software without restriction, 
/// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
/// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
/// subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial 
/// portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
/// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
/// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
/// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///

using System;
using System.Collections.Generic;
using System.Text;

namespace com.feelgoodsoftware.sharptftp.utilities
{
    /// <summary>
    /// Utility class used to manipulate byte arrays.
    /// </summary>
    public static class ByteArray
    {
        /// <summary>
        /// Utility method to represent a hexadecimal string as a byte array.
        /// </summary>
        /// <param name="HexString">The hex string to respresent.</param>
        /// <returns>The byte array representation.</returns>
        public static byte[] FromHex(string HexString)
        {
            int NumberChars = HexString.Length;
            byte[] bytes;

            try
            {
                bytes = new byte[NumberChars / 2];
                for (int i = 0; i < NumberChars; i += 2)
                {
                    bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
                }
            }
            catch
            {
                bytes = new byte[0];
            }
            return bytes;
        }

        /// <summary>
        /// Utility method to represent a byte array as a hex string.
        /// </summary>
        /// <param name="ByteArray">The byte array to represent.</param>
        /// <returns>The hex string.</returns>
        public static string ToHex(byte[] ByteArray)
        {
            string hexString = "";

            for (int i = 0; i < ByteArray.Length; i++)
            {
                hexString += ByteArray[i].ToString("X2");
            }

            return hexString;
        }

        /// <summary>
        /// Combine two byte arrays.
        /// </summary>
        /// <param name="a">The first array.</param>
        /// <param name="b">The second array.</param>
        /// <returns>A combined byte array.</returns>
        public static byte[] Combine(byte[] a, byte[] b)
        {
            byte[] c = new byte[a.Length + b.Length];
            System.Buffer.BlockCopy(a, 0, c, 0, a.Length);
            System.Buffer.BlockCopy(b, 0, c, a.Length, b.Length);
            return c;
        }
    }
}
